/**
 * Created by Ysssssss on 2021/5/17.
 */

import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    OPEN_SHOPPING_MALL,
    EDIT_SHOPPING_MALL_SETTINGS,
    GET_SHOPPING_MALL_SETTINGS,
    GET_SHOPPING_MALL_DESIGN_INFO,
    EDIT_SHOPPING_MALL_DESIGN_INFO
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios} from "@utils/api";

/**
 * 开通商城
 * @param bizType
 * @param mallConfig
 * @param callback
 * @returns {{callback: *, type: string, params: {bizType: *}}}
 */
export const openShoppingMall = ({ bizType, ...mallConfig }, callback) => {
    return {
        type: apiType(OPEN_SHOPPING_MALL).request,
        params: {
            bizType,
            ...mallConfig
        },
        callback
    }
}

/**
 * 获取商城配置
 * @param callback
 * @returns {{callback: *, type: string}}
 */
export const getShoppingMallSettings = (callback) => {
    return {
        type: apiType(GET_SHOPPING_MALL_SETTINGS).request,
        callback
    }
}

/**
 * 修改商城配置
 * @param mallConfig
 * @param callback
 * @returns {{callback: *, type: string, params: *}}
 */
export const editShoppingMallSettings = (mallConfig, callback) => {
    return {
        type: apiType(EDIT_SHOPPING_MALL_SETTINGS).request,
        params: mallConfig,
        callback
    }
}

/**
 * 获取商城装修
 * @param platformId
 * @param callback
 * @returns {{callback: *, type: string, params: {platformId: *}}}
 */
export const getShoppingMallDesignInfo = ({ sourceType }, callback) => ({
    type: apiType(GET_SHOPPING_MALL_DESIGN_INFO).request,
    params: { sourceType },
    callback
})

/**
 * 修改商城装修
 * @param platformId
 * @param decorationInfo
 * @param prodIds
 * @param callback
 * @returns {{callback: *, type: string, params: {decorationInfo: *, platformId: *, prodIds: *}}}
 */
export const editShoppingMallDesignInfo = ({ sourceType, decorationInfo, prodIds }, callback) => ({
    type: apiType(EDIT_SHOPPING_MALL_DESIGN_INFO).request,
    params: {
        sourceType,
        decorationInfo,
        prodIds
    },
    callback
})

const sagas = {
    * [apiType(GET_SHOPPING_MALL_SETTINGS).request]({callback}) {
        try {
            const response = yield getAxios('/mall/web/config')
            yield put({
                type: apiType(GET_SHOPPING_MALL_SETTINGS).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(OPEN_SHOPPING_MALL).failure});
        }
    },
    * [apiType(OPEN_SHOPPING_MALL).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/config', params)
            yield put({
                type: apiType(OPEN_SHOPPING_MALL).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(OPEN_SHOPPING_MALL).failure});
        }
    },
    * [apiType(EDIT_SHOPPING_MALL_SETTINGS).request]({params, callback}) {
        try {
            const response = yield putAxios('/mall/web/config', params)
            yield put({
                type: apiType(EDIT_SHOPPING_MALL_SETTINGS).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(EDIT_SHOPPING_MALL_SETTINGS).failure});
        }
    },
    * [apiType(GET_SHOPPING_MALL_DESIGN_INFO).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/decoration', params)
            yield put({
                type: apiType(GET_SHOPPING_MALL_DESIGN_INFO).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({type: apiType(GET_SHOPPING_MALL_DESIGN_INFO).failure});
        }
    },
    * [apiType(EDIT_SHOPPING_MALL_DESIGN_INFO).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/decoration', params)
            yield put({
                type: apiType(EDIT_SHOPPING_MALL_DESIGN_INFO).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(EDIT_SHOPPING_MALL_DESIGN_INFO).failure});
        }
    },
}


export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})